﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "MoveWindowDoorCmd.h"


#include "IUiDocumentViewManager.h"
#include "IUiView.h"
#include "MoveWindowDoorAction.h"

#include "IModelView.h"
#include "ICommandManager.h"
#include "CommandParameters.h"
#include "GcmpCommandState.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

namespace {
    gcmp::CommandParameterValue GetCmdParam(const gcmp::CommandParameters& cmdParams, const std::wstring& key)
    {
        auto itor = cmdParams.find(key);
        DBG_WARN_AND_RETURN_UNLESS(itor != cmdParams.end(), gcmp::CommandParameterValue(), L"没有找到所需参数",L"GDMPLab",L"2024-03-30");
        return itor->second;
    }
}

MoveWindowDoorCmd::MoveWindowDoorCmd()
    : CommandBase(L"gmMoveWindowDoorCmd")
{
}


MoveWindowDoorCmd::~MoveWindowDoorCmd()
{
}

bool MoveWindowDoorCmd::IsEnabled() const
{
    if(!CommandBase::IsEnabled())
        return false;

    IUiView* curUIView = IUiDocumentViewManager::Get()->GetCurrentUiView();
    if (curUIView == nullptr)
        return false;

    IModelView* curModelView = curUIView->GetModelView();
    if (curModelView == nullptr)
        return false;

    if (L"PlanViewType" != curModelView->GetUserDataId())

        return false;
    return true;
}

bool MoveWindowDoorCmd::IsVisible() const
{
    return gcmp::GcmpCommandState::IsInProjectEnvironment() && !gcmp::GcmpCommandState::IsInAnimationEnvironment();
}

gcmp::OwnerPtr<IAction> MoveWindowDoorCmd::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    if (cmdParams.size() == 4)
    {
        Int64 modedId = GetCmdParam(cmdParams, L"ElementId").AsInt64();
        double x,y,z;
        x = GetCmdParam(cmdParams, L"Vector3d_X").AsDouble();
        y = GetCmdParam(cmdParams, L"Vector3d_Y").AsDouble();
        z = GetCmdParam(cmdParams, L"Vector3d_Z").AsDouble();
        Vector3d startPt(x,y,z);
        if (modedId > 0)
        {
            return NEW_AS_OWNER_PTR(MoveWindowDoorAction, ElementId(modedId), startPt);
        }
    }
    else if (cmdParams.size() == 2)
    {
        Int64 modedId = GetCmdParam(cmdParams, L"ElementId").AsInt64();
        if (modedId > 0)
        {
            return NEW_AS_OWNER_PTR(MoveWindowDoorAction, ElementId(modedId));
        }
    }
    else
    {
        DBG_WARN(L"参数数量不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

void RegisterMoveWindowDoorCmd()
{
    ICommandManager::Get()->RegisterCommand(TR_MODULE_NAME, NEW_AS_OWNER_PTR(MoveWindowDoorCmd));
}
ONE_TIME_INITIALIZER(RegisterMoveWindowDoorCmd)


